package com.example.teatea.dao;

import com.example.teatea.project.Good;
import com.example.teatea.project.Member;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DoFindGoods {
    public List<Good> findGoodsByName(String key){
        List<Good> goods = new ArrayList<>();
        Connection connection = null;
        ResultSet resultSet = null;
        Properties properties = new Properties();
        PreparedStatement presta = null;
        StringBuilder stringBuilder = new StringBuilder();//定义查询条件字符串
        if("".equals(key)){//未输入查询关键字，显示所有数据
            stringBuilder = stringBuilder.append("1=1");
        }else{//拼接按姓名和手机号模糊查询的条件字符串
            stringBuilder.append("gName like '%").append(key).append("%'");
        }
        try {
            properties.load(new FileInputStream("src\\main\\resources\\com\\example\\teatea\\sql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String Driver = properties.getProperty("Driver");
            String url = properties.getProperty("url");
            Class.forName(Driver);
            String sql = "select gName,gPrice,gNumber,gType,gPhoto from goods where " + stringBuilder.toString();
            connection = DriverManager.getConnection(url,user,password);
            presta = connection.prepareStatement(sql);
            resultSet = presta.executeQuery(sql);
            while (resultSet.next()){
                String name = resultSet.getString("gName");
                float price = resultSet.getFloat("gPrice");
                int num = resultSet.getInt("gNumber");
                int t = resultSet.getInt("gType");
                String type = new String();
                switch (t){
                    case 1: type = "茶";break;
                    case 2: type = "咖啡";break;
                    case 3: type = "便餐";break;
                    case 4: type = "娱乐项目";break;
                }
                String photo = resultSet.getString("gPhoto");
                goods.add(new Good(name,price,num,type,photo));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return goods;
    }

    public List<Good> findGoodsByType(String key){
        int keykey = 0;
        if(key.equals("茶")) keykey = 1;
        else if(key.equals("咖") || key.equals("咖啡") || key.equals("啡")) keykey = 2;
        else if(key.equals("便") || key.equals("餐") || key.equals("便餐")) keykey = 3;
        else keykey = 4;
        List<Good> goods = new ArrayList<>();
        Connection connection = null;
        ResultSet resultSet = null;
        Properties properties = new Properties();
        PreparedStatement presta = null;
        StringBuilder stringBuilder = new StringBuilder();//定义查询条件字符串
        if("".equals(key)){//未输入查询关键字，显示所有数据
            stringBuilder = stringBuilder.append("1=1");
        }else{//拼接按姓名和手机号模糊查询的条件字符串
            stringBuilder.append("gType like ").append(keykey);
        }
        try {
            properties.load(new FileInputStream("src\\main\\resources\\com\\example\\teatea\\sql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String Driver = properties.getProperty("Driver");
            String url = properties.getProperty("url");
            Class.forName(Driver);
            String sql = "select gName,gPrice,gNumber,gType,gPhoto from goods where " + stringBuilder.toString();
            connection = DriverManager.getConnection(url,user,password);
            presta = connection.prepareStatement(sql);
            resultSet = presta.executeQuery(sql);
            while (resultSet.next()){
                String name = resultSet.getString("gName");
                float price = resultSet.getFloat("gPrice");
                int num = resultSet.getInt("gNumber");
                int t = resultSet.getInt("gType");
                String type = new String();
                switch (t){
                    case 1: type = "茶";break;
                    case 2: type = "咖啡";break;
                    case 3: type = "便餐";break;
                    case 4: type = "娱乐项目";break;
                }
                String photo = resultSet.getString("gPhoto");
                goods.add(new Good(name,price,num,type,photo));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return goods;
    }
}
